package com.jmu.edu.IMApplication.fragment;

import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jmu.edu.IMApplication.R;
import com.jmu.edu.IMApplication.adapter.AlbumItemAdapter;
import com.jmu.edu.IMApplication.utils.AlbumUtils;
import com.jmu.edu.IMApplication.utils.ImageTools;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class AlbumFragment extends Fragment {

    private List<File> fileList;
    private RecyclerView recyclerView;
    private AlbumItemAdapter adapter;
    private static Cursor cursor=null;

    public AlbumFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_album, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        recyclerView = getView().findViewById(R.id.fragment_album_recyclerview);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
        recyclerView.setLayoutManager(gridLayoutManager);
        loadData();
    }

    private void loadData() {
        //fileList = AlbumUtils.getPictureFile(getActivity());
        //重载了获取函数，新增加载的数量
        fileList=AlbumUtils.getPictureFile(getActivity(),cursor,50);
       //fileList=AlbumUtils.getPictureFile(getActivity(),cursor,15);
        if (adapter == null) {
            adapter = new AlbumItemAdapter();
        }
        adapter.setFileList(fileList);
        // 如果还没有添加适配器，那么设置适配器，否则适配器提醒recyclerview进行数据更新
        if (recyclerView.getAdapter() == null) {
            recyclerView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
    }

    /*
    获取图片bitmap
     *//*
    private List<Bitmap> getPictureBitmap() {
        List<Bitmap> bitmapList = new ArrayList<>();
        List<String> Paths = getPicturePath();
        for (int i = Paths.size() - 1; i > 0; i--) {
            Bitmap bitmap = ImageTools.getBitmap(Paths.get(i));
            bitmapList.add(bitmap);
        }
        return bitmapList;
    }

    *//*
    获取图片Path路径
     *//*
    private List<String> getPicturePath() {
        List<String> paths = new ArrayList<>();
        Uri ImagesUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;//获取照片地址uri
        ContentResolver contentResolver = getActivity().getContentResolver();//创建内容解析者
        Cursor cursor = contentResolver.query(ImagesUri, null  //数据库查询
                , MediaStore.Images.Media.MIME_TYPE + "=? or " + MediaStore.Images.Media.MIME_TYPE + "=?"
                , new String[]{"image/jpeg", "image/png"}
                , MediaStore.Images.Media.DATE_MODIFIED);
        //移动到近10张照片
        cursor.moveToPosition(cursor.getCount() - 10);
        while (cursor.moveToNext()) {
            String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            paths.add(path);
        }
        cursor.close();
        return paths;
    }*/

}